//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using Codingly.SvnExternalUpdater.Core.Services;
using Codingly.SvnExternalUpdater.Model;
using Codingly.SvnExternalUpdater.Mvp;
using Codingly.SvnExternalUpdater.Services;
using Codingly.SvnExternalUpdater.Views;

namespace Codingly.SvnExternalUpdater.Presenters
{
	public class ConfigurationPresenter : AbstractPresenter<IConfigurationView, ConfigurationPresenter>
	{
		private readonly IConfigurationService configurationService;
		private readonly IVcsService vcsService;

		private MonitorConfiguration backupConfiguration;
		private MonitorConfiguration configuration;
		private bool configurationWasModified;

		public ConfigurationPresenter(IConfigurationService configurationService, IVcsService vcsService)
		{
			this.configurationService = configurationService;
			this.vcsService = vcsService;
		}

		public MonitorConfiguration Configuration
		{
			get { return this.configuration; }
		}

		public bool ConfigurationWasModified
		{
			get { return this.configurationWasModified; }
		}

		public bool AddProject(string project)
		{
			if (this.configuration.Projects.Contains(project))
			{
				return false;
			}

			if (!this.vcsService.IsValidTarget(project))
			{
				return false;
			}

			this.configuration.Projects.Add(project);
			this.view.AddProject(project);
			return true;
		}

		public void Execute()
		{
			this.configuration = this.configurationService.GetConfiguration();
			this.backupConfiguration = this.configuration.Clone() as MonitorConfiguration;

			foreach (string project in this.configuration.Projects)
			{
				this.view.AddProject(project);
			}

			if (this.view.ShowModal())
			{
				this.configurationService.SaveConfiguration(this.configuration);
				this.configurationWasModified = true;
			}
			else
			{
				this.configurationWasModified = true;
				this.configuration = this.backupConfiguration;
			}
		}

		public void RemoveProject(string project)
		{
			this.configuration.Projects.Remove(project);
			this.view.RemoveProject(project);
		}
	}
}